#include "hplcsparklinkautosamplerdeviceprotocol.h"
#include "hplcsparklinkautosamplerdevicesendpacket.h"
#include "hplcsparklinkautosamplerdeviceparsepacket.h"
#include "hplcsparklinkautosamplerprotocoldata.h"

#include "hplcsparklinkmakepacket.h"
#include "hplcsparklinkfindpacket.h"
#include "hplcsparklinkcheckpacket.h"

namespace hplc
{
namespace sparklink
{

HplcSparkLinkAutoSamplerDeviceProtocol::HplcSparkLinkAutoSamplerDeviceProtocol(AutoSamplerParam &param, QObject *parent)
    : HplcSparkLinkAutoSamplerDeviceProtocol (*new HplcSparkLinkAutoSamplerProtocolData(), param, parent)
{

}

HplcSparkLinkAutoSamplerDeviceProtocol::HplcSparkLinkAutoSamplerDeviceProtocol(HplcSparkLinkAutoSamplerProtocolData &data, AutoSamplerParam &param, QObject *parent)
    : BaseProtocol (data, parent)
    , m_param(&param)
{
    data.m_id = 61; // 61为自动进样器的编号

    HplcSparkLinkMakePacket *maker = new HplcSparkLinkMakePacket(data);
    HplcSparkLinkCheckPacket *checker = new HplcSparkLinkCheckPacket(data, this);

    data.m_maker = maker;
    data.m_checker = checker;

    HplcSparkLinkAutoSamplerDeviceParsePacket *parser = new HplcSparkLinkAutoSamplerDeviceParsePacket(data, param, this);
    connect(parser, &BaseParsePacket::parseCompleted, this, &BaseProtocol::parseCompleted);
    HplcSparkLinkAutoSamplerDeviceSendPacket *sender = new HplcSparkLinkAutoSamplerDeviceSendPacket (data, this);
    connect(sender, &BaseSendPacket::requestSend, this, &BaseProtocol::requestSend);

    data.m_parser = parser;
    data.m_sender = sender;
}

HplcSparkLinkAutoSamplerDeviceProtocol::~HplcSparkLinkAutoSamplerDeviceProtocol()
{
    HplcSparkLinkAutoSamplerProtocolData *d = static_cast<HplcSparkLinkAutoSamplerProtocolData*>(m_d);
    delete d->m_sender;
    delete d->m_parser;
    delete d->m_checker;
    delete d->m_maker;
}

HplcSparkLinkAutoSamplerDeviceSendPacket *HplcSparkLinkAutoSamplerDeviceProtocol::getSender()
{
    return static_cast<HplcSparkLinkAutoSamplerDeviceSendPacket*>(m_d->m_sender);
}

HplcSparkLinkAutoSamplerDeviceParsePacket *HplcSparkLinkAutoSamplerDeviceProtocol::getParser()
{
    return static_cast<HplcSparkLinkAutoSamplerDeviceParsePacket*>(m_d->m_parser);
}

ProtocolType HplcSparkLinkAutoSamplerDeviceProtocol::getProtocolType()
{
    return ProtocolType_SparkLink_AutoSampler;
}

void HplcSparkLinkAutoSamplerDeviceProtocol::onBaseTickTimeout()
{
    HplcSparkLinkProtocolCommonData *m_d = static_cast<HplcSparkLinkProtocolCommonData*>(this->m_d);
    m_d->m_checker->onBaseTickTimeout();
}

} // sparklink
} // hplc
